package ssi.tools;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmail {
	
	
	public void sendActivationEmail(String email, String activationCode){
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");
 
		Session session = Session.getDefaultInstance(props,
			new javax.mail.Authenticator() {
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication("ssiproject2013@gmail.com","inzynieria2013");
				}
			});
 
		try {
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("test@x-schronisko.com"));
			message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email));
			message.setSubject("x-schronisko - aktywacja konta");
			message.setText("Witaj na x-schronisko! \n\n Aby dokonczyc rejestracje kliknij w link: http://localhost:8080/SSIProject/ActivationServlet?code="+activationCode);			
			Transport.send(message);
			 
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}
	
	public void sendPasswordEmail(String email, String password){
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");
 
		Session session = Session.getDefaultInstance(props,
			new javax.mail.Authenticator() {
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication("ssiproject2013@gmail.com","inzynieria2013");
				}
			});
 
		try {
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("test@x-schronisko.com"));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(email));
			message.setSubject("Przypomnienie hasla");
			message.setText("Witaj! \n\n Oto Twoje nowe haslo do portalu x-schronisko: "+password+" \n\n Pozdrawiamy \n x-schronisko team.");			
			Transport.send(message);
 
			System.out.println("Done");
 
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}
	
	public void sendDeleteEmail(String email, String deleteCode) {
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");
 
		Session session = Session.getDefaultInstance(props,
			new javax.mail.Authenticator() {
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication("ssiproject2013@gmail.com","inzynieria2013");
				}
			});
 
		try {
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("test@x-schronisko.com"));
			message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email));
			message.setSubject("x-schronisko - usuniecie konta");
			message.setText("Witaj na x-schronisko! \n\n Aby potwierdzic usuniecie konta klinkij w ponizszy link: http://localhost:8080/SSIProject/DeleteAccountServlet?code="+deleteCode + "&mail=" + email);			
			Transport.send(message);
			 
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}
	
	
	public void sendAdoptionEmail(String email, String activationCode) {
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");
 
		Session session = Session.getDefaultInstance(props,
			new javax.mail.Authenticator() {
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication("ssiproject2013@gmail.com","inzynieria2013");
				}
			});
 
		try {
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("test@x-schronisko.com"));
			message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email));
			message.setSubject("x-schronisko - adopcja zwierzecia");
			message.setText("Witaj na x-schronisko! \n\n Aby dokonczyc proces adopcyjny kliknij w link: http://localhost:8080/SSIProject/AdoptionServlet?code="+activationCode);			
			Transport.send(message);
			 
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}
}

